﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;

using MySql.Data.MySqlClient;
using Think.Lenovo.Com.Models.Users;
using Think.Lenovo.Com.Common;
namespace Think.Lenovo.Com.DataAccess.User
{
    public class UserState
    {
        /// <summary>
        /// 添加用户状态
        /// </summary>
        /// <param name="model">用户状态实体对象</param>
        /// <returns>自增id</returns>
        public long AddUserState(UserStateModel model)
        {
            #region //添加用户状态

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append("insert into ")
                .Append(" tbl_userstate ")
                .Append(" (Id, userStateName) ")
                .Append(" values ")
                .Append(" (@Id, @userStateName) ");

            #endregion

            #region//Sql参数数据
            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@Id",MySqlDbType.Int32),
                    new MySqlParameter("@userStateName",MySqlDbType.VarChar,10)
                };

            ps[0].Value = model.Id;
            ps[1].Value = model.UserStateName;
            #endregion
            long id = MysqlHelper.Insert(conn, sql.ToString(), ps);
            return id;

            #endregion
        }

        public List<UserStateModel> GetUserStates()
        {
            List<UserStateModel> list = new List<UserStateModel>();
            #region //

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            #region//sql语句
            StringBuilder sql = new StringBuilder(128);
            sql.Append(" Select * From ")
                .Append(" tbl_userstate ");
            #endregion

            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            if (ds != null)
            {
                if (ds.Tables[0] != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            UserStateModel model = new UserStateModel();
                            model.Id = Convert.ToInt32(row["Id"]);
                            model.UserStateName = Convert.ToString(row["userStateName"]);
                            list.Add(model);
                        }
                    }
                }
            }
            return list;

            #endregion
        }
    }
}
